package cn.xh.basic.service.impl;

import cn.xh.basic.mapper.BaseMapper;
import cn.xh.basic.query.BaseQuery;
import cn.xh.basic.service.IBaseService;
import cn.xh.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 基础的公共接口的实现类
 */
//@Service  应该交给子类继承（子类才应该由容器管理），不应该由容器管理
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> mapper;

    @Transactional
    @Override
    public void add(T t) {
        mapper.save(t);
    }

    @Transactional
    @Override
    public void update(T t) {
        mapper.update(t);
    }

    @Transactional
    @Override
    public void remove(Long id) {
        mapper.delete(id);
    }

    @Override
    public T queryById(Long id) {
        return mapper.loadById(id);
    }

    @Override
    public List<T> queryAll() {
        return mapper.loadAll();
    }

    @Override
    public PageList queryPage(BaseQuery query) {
        //查询出总条数
        Integer total = mapper.loadCount(query);
        //查询每页数据
        List<T> rows = mapper.loadPage(query);
        return new PageList(total, rows);
    }

    @Transactional
    @Override
    public void patchDelete(List<Long> ids) {
        mapper.patchDelete(ids);
    }
}
